package ywpt.web.action.client;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.ecside.table.limit.Limit;
import org.ecside.util.RequestUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import ywpt.components.acegi.domain.Operation;
import ywpt.core.Constants;
import ywpt.core.SystemMessage;
import ywpt.core.exception.UniqueConstraintException;
import ywpt.core.util.DateTools;
import ywpt.core.util.StringTools;
import ywpt.core.web.BaseAction;
import ywpt.model.ClientVisit;
import ywpt.model.JobInfo;
import ywpt.model.RepairInfo;
import ywpt.service.client.ClientVisitService;
import ywpt.service.service.JobInfoService;
import ywpt.service.service.RepairInfoService;
import ywpt.web.common.CommonDictionary;
import ywpt.web.common.OperationConstants;
import ywpt.web.common.PublicFunction;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class ClientVisitAction extends BaseAction implements ModelDriven<ClientVisit>, Preparable{

	/**
	 * 客户回访Action
	 */
	private static final long serialVersionUID = 1376267702260682311L;
	private boolean[] popedom=new boolean[Constants.POPEDOM_LEN];//λȨ�����飬	
	private static final SimpleDateFormat FORMAT = new SimpleDateFormat("yyyy-MM-dd");
	private static int DEFAULT_PAGE_SIZE = 20;
	private Long id;
	private ClientVisit clientVisit;
	private List<ClientVisit> clientVisitList = new ArrayList<ClientVisit>();
	private ClientVisitService clientVisitService;
	
	private JobInfo jobInfo;
	private JobInfoService jobInfoService;
	
	private RepairInfo repairInfo;
	private RepairInfoService repairInfoService;

	
	//��ѯ���
	private String qCode;
	private String qServiceCode;
	private String qVisitMan;
	private String qStartDate;
	private String qEndDate;
 	
	private Long jobId;
	private Long repairId;
	private String repairName;
	private Long clientId;
	private String clientName;
	
	public Long getRepairId() {
		return repairId;
	}

	public void setRepairId(Long repairId) {
		this.repairId = repairId;
	}

	public String getRepairName() {
		return repairName;
	}

	public void setRepairName(String repairName) {
		this.repairName = repairName;
	}

	public Long getClientId() {
		return clientId;
	}

	public void setClientId(Long clientId) {
		this.clientId = clientId;
	}

	public String getClientName() {
		return clientName;
	}

	public void setClientName(String clientName) {
		this.clientName = clientName;
	}

	public Long getJobId() {
		return jobId;
	}

	public void setJobId(Long jobId) {
		this.jobId = jobId;
	}

	public String getQCode() {
		return qCode;
	}

	public void setQCode(String code) {
		qCode = code;
	}

	public String getQServiceCode() {
		return qServiceCode;
	}

	public void setQServiceCode(String serviceCode) {
		qServiceCode = serviceCode;
	}

	public String getQVisitMan() {
		return qVisitMan;
	}

	public void setQVisitMan(String visitMan) {
		qVisitMan = visitMan;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public ClientVisit getClientVisit() {
		return clientVisit;
	}

	public void setClientVisit(ClientVisit clientVisit) {
		this.clientVisit = clientVisit;
	}

	public List<ClientVisit> getClientVisitList() {
		return clientVisitList;
	}

	public void setClientVisitList(List<ClientVisit> clientVisitList) {
		this.clientVisitList = clientVisitList;
	}

	public ClientVisitService getClientVisitService() {
		return clientVisitService;
	}

	public void setClientVisitService(ClientVisitService clientVisitService) {
		this.clientVisitService = clientVisitService;
	}
	public boolean[] getPopedom() {
		return popedom;
	}

	public void setPopedom(boolean[] popedom) {
		this.popedom = popedom;
	}

	public JobInfo getJobInfo() {
		return jobInfo;
	}

	public void setJobInfo(JobInfo jobInfo) {
		this.jobInfo = jobInfo;
	}

	public JobInfoService getJobInfoService() {
		return jobInfoService;
	}

	public void setJobInfoService(JobInfoService jobInfoService) {
		this.jobInfoService = jobInfoService;
	}	
//*****************************************************************************
	@Override
	public ClientVisit getModel() {
		return this.clientVisit;
	}

	@Override
	public void prepare() throws Exception {
		if(id != null){
			this.clientVisit = this.clientVisitService.getClientVisitById(id);
		}else{
			this.clientVisit = new ClientVisit();
		}
		HashMap<String,Operation> oper=this.getOperMapByUser(this.getCurrentUser());
		PublicFunction.getOperationPopedom(oper,popedom,OperationConstants.SERVICE_CLIENT_VISIT);
	}
	protected void showInfo() {
		if (this.clientVisit.getJobInfo()!=null) {
			this.jobId = this.clientVisit.getJobInfo().getId();
		}
		if (this.clientVisit.getRepair()!=null) {
			this.repairId = this.clientVisit.getRepair().getId();
			this.repairName=this.clientVisit.getRepair().getCode();
		}
		if (this.clientVisit.getClient()!=null) {
			this.clientId=this.clientVisit.getClient().getId();
			this.clientName=this.clientVisit.getClient().getClientName();
		}
	}
	//*****************************************************************************
	@SuppressWarnings("unchecked")
	public String list() {
		DetachedCriteria query = DetachedCriteria.forClass(ClientVisit.class);
		int totalRows = RequestUtils.getTotalRowsFromRequest(getRequest());
		if (totalRows < 0) {
			totalRows = clientVisitService.getClientVisitCountByCriteria(query);
		}
		int[] rowStartEnd = RequestUtils.getRowStartEnd(getRequest(),
				totalRows, DEFAULT_PAGE_SIZE, 0);
		Limit limit = RequestUtils.getLimit(getRequest(), "ec", totalRows,
				DEFAULT_PAGE_SIZE);
		int pageSize = limit.getCurrentRowsDisplayed();
		query.addOrder(Order.desc("code"));
		query.setProjection(null);
		clientVisitList.addAll(clientVisitService.getClientVisits(query, pageSize, rowStartEnd[0]));
		getRequest().setAttribute("PAYMETHOD_MAP", CommonDictionary.PAYMETHOD);
		return SUCCESS;
	}

	public String input() 
	{	
		clientVisit.setVisitDate(new Date());
		if (this.repairId!=null) {
			this.repairInfo = this.repairInfoService.getRepairInfoById(repairId);
			this.repairId = repairInfo.getId();
			this.repairName = repairInfo.getCode();
			this.clientId = repairInfo.getClient().getId();
			this.clientName = repairInfo.getClient().getClientName();
			clientVisit.setLinkUser(repairInfo.getLinkUser());
			clientVisit.setVisitContent(repairInfo.getFaultDesc());
		}
		return INPUT;
	}
	
	public String edit() 
	{
		this.clientVisit=this.clientVisitService.getClientVisitById(id);
		showInfo();
		return INPUT;
	}
	
	/**
	 * ����
	 * 
	 * @return
	 */
	public String save() throws Exception 
	{
		try 
		{
			
			if (id == null) 
			{
				clientVisit.setCode(clientVisitService.createClientVisitCode());
				clientVisitService.saveClientVisit(clientVisit,jobId,repairId,clientId);
				if (jobId!=null) {
					jobInfo = jobInfoService.getJobInfoById(jobId);
					jobInfo.setVisitFlag(true);
					jobInfoService.updateJobInfo(jobInfo, null, null, null, null, null, null, null, null, null, null, null, null);
				}
				this.addActionMessage(SystemMessage.SAVE_SUCCESS);
			} 
			else 
			{
				clientVisitService.updatClientVisit(clientVisit,jobId,repairId,clientId);
				this.addActionMessage(SystemMessage.UPDATE_SUCCESS);
			}
			showInfo();
			return SUCCESS;
		} 
		catch (UniqueConstraintException e) 
		{
			this.addActionError(SystemMessage.SAVE_FAIL + e.getMessage());
			return INPUT;
		}
	}
	
	/**
	 * ɾ��
	 * 
	 * @return
	 */
	public String delete() throws Exception {		
		if (id != null) 
		{	
			clientVisitService.removeClientVisit(id);
			return SUCCESS;				
		}else {
			this.addActionMessage(SystemMessage.NO_OBJECT);
			return INPUT;
		}
	}

	public String search() throws Exception 
	{
		DetachedCriteria query = DetachedCriteria.forClass(ClientVisit.class);
		if(!StringTools.isNullOrSpace(this.qCode)){
			query.add(Restrictions.like("code", "%"+this.qCode+"%"));
		}
		if(!StringTools.isNullOrSpace(this.qServiceCode)){
			query.createAlias("jobInfo", "jobInfo").add(Restrictions.like("jobInfo.jobCode", "%"+this.qServiceCode+"%"));
		}	
		if(!StringTools.isNullOrSpace(this.qStartDate)&& !StringTools.isNullOrSpace(this.qEndDate))
		{				
			if(FORMAT.parse(this.qStartDate).before(FORMAT.parse(this.qEndDate)))
				query.add(Restrictions.between("visitDate",FORMAT.parse(this.qStartDate),DateTools.addDay(FORMAT.parse(this.qEndDate), 0)));
			else  
				query.add(Restrictions.between("visitDate",FORMAT.parse(this.qEndDate),DateTools.addDay(FORMAT.parse(this.qStartDate),0)));
		}
		if(!StringTools.isNullOrSpace(this.qVisitMan)){
			query.add(Restrictions.like("visitMan", "%"+this.qVisitMan+"%"));
		} 
		int totalRows = RequestUtils.getTotalRowsFromRequest(getRequest());
		if (totalRows < 0) 
		{
			totalRows = clientVisitService.getClientVisitCountByCriteria(query);
		}
		int[] rowStartEnd = RequestUtils.getRowStartEnd(getRequest(),
				totalRows, DEFAULT_PAGE_SIZE, 0);
		Limit limit = RequestUtils.getLimit(getRequest(), "ec", totalRows,
				DEFAULT_PAGE_SIZE);
		int pageSize = limit.getCurrentRowsDisplayed();
		query.addOrder(Order.desc("code"));
		query.setProjection(null);
		clientVisitList.addAll(clientVisitService.getClientVisits(query, pageSize, rowStartEnd[0]));
		return SUCCESS;
	}
	
	public String select() 
	{
		clientVisitList.addAll(clientVisitService.getClientVisits());
		return SUCCESS;
	}

	public RepairInfo getRepairInfo() {
		return repairInfo;
	}

	public void setRepairInfo(RepairInfo repairInfo) {
		this.repairInfo = repairInfo;
	}

	public RepairInfoService getRepairInfoService() {
		return repairInfoService;
	}

	public void setRepairInfoService(RepairInfoService repairInfoService) {
		this.repairInfoService = repairInfoService;
	}

	public String getQStartDate() {
		return qStartDate;
	}

	public void setQStartDate(String startDate) {
		qStartDate = startDate;
	}

	public String getQEndDate() {
		return qEndDate;
	}

	public void setQEndDate(String endDate) {
		qEndDate = endDate;
	}


	
}
